//
//  RecordingsController.m
//  SoundRecording
//
//  Created by zhengju on 2017/11/29.
//  Copyright © 2017年 郑俱. All rights reserved.
//

#import "RecordingsController.h"
#import "AudioObject.h"
#import "ZJRecorder.h"
#import "ZJAudioPlayer.h"
@interface RecordingsController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    NSFileManager *fileMgr;
    NSString *dateaudioPath;
    NSString *audioRecoderSavePath;
    NSMutableArray *audioRcoderMutableArray;
}
@property(strong,nonatomic) UITableView * tableView;
@end

@implementation RecordingsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"录音列表";
    
    self.view.backgroundColor = [UIColor yellowColor];
    [self configureTableView];
    [self filePath];
}
- (void)configureTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.view addSubview:self.tableView];
}
- (void)filePath{
    
    NSDate *  date=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    NSString *datefloder= [dateformatter stringFromDate:date];
    dateaudioPath=[NSString stringWithFormat:@"%@/",datefloder];
    fileMgr = [NSFileManager defaultManager];
    
    NSString * audioPath = @"zhengju";
    
    //指向文件目录
    NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    audioRecoderSavePath=[NSString stringWithFormat:@"%@/%@%@", documentsDirectory,audioPath,dateaudioPath];
    
    if (![fileMgr fileExistsAtPath:audioRecoderSavePath]) {
        [fileMgr createDirectoryAtPath:audioRecoderSavePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    audioRcoderMutableArray=[[NSMutableArray alloc]init];
    
    audioRcoderMutableArray = [self getFilenamelistOfType:@"caf" fromDirPath:audioRecoderSavePath];
}
-(NSMutableArray *)getFilenamelistOfType:(NSString *)type fromDirPath:(NSString *)dirPath
{
    NSMutableArray *filenamelist = [[NSMutableArray alloc]init];
    NSArray *tmplist = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil];
    
    for (NSString *filename in tmplist) {
        NSString *fullpath = [dirPath stringByAppendingPathComponent:filename];
        if ([self isFileExistAtPath:fullpath]) {
            if ([[filename pathExtension] isEqualToString:type]) {
                AudioObject *ob=[[AudioObject alloc]init];
                ob.audioRecoderName=filename;
                ob.audioRecoderPath=fullpath;
                //获取文件时长
                AVURLAsset* audioAsset =[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:fullpath] options:nil];
                CMTime audioDuration = audioAsset.duration;
                float audioDurationSeconds =CMTimeGetSeconds(audioDuration);
                
                NSLog(@"%f",audioDurationSeconds);

                ob.audioRecoderIsChecked=NO;
                [filenamelist addObject:ob];
            }
        }
    }
    
    return filenamelist;
}
-(BOOL)isFileExistAtPath:(NSString*)fileFullPath {
    BOOL isExist = NO;
    isExist = [[NSFileManager defaultManager] fileExistsAtPath:fileFullPath];
    return isExist;
}
#pragma UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return audioRcoderMutableArray.count;;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = @"证据来了";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    AudioObject *zz=[audioRcoderMutableArray objectAtIndex:indexPath.row];
    NSString * deletefile=zz.audioRecoderPath;
    
    BOOL bRet = [fileMgr fileExistsAtPath:deletefile];
    if (bRet) {
       
        NSError *err;
        [fileMgr removeItemAtPath:deletefile error:&err];
        
    }
    [ audioRcoderMutableArray removeObjectAtIndex:indexPath.row];
   
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
}
//修改左滑删除按钮的title
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
#pragma UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AudioObject * model = audioRcoderMutableArray[indexPath.row];
    [[ZJAudioPlayer sharePlayer]recorderSetPath:model.audioRecoderPath];
    [[ZJAudioPlayer sharePlayer]recorderPlay];
}
@end
